package com.ruoyi.rms.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rms.domain.OrderMessage;
import com.ruoyi.rms.service.IOrderMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 * 
 * @author wyl
 * @date 2024-03-08
 */
@RestController
@RequestMapping("/rms/order")
public class OrderMessageController extends BaseController
{
    @Autowired
    private IOrderMessageService orderMessageService;

    /**
     * 查询订单信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rms:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderMessage orderMessage)
    {
        startPage();
        List<OrderMessage> list = orderMessageService.selectOrderMessageList(orderMessage);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rms:order:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderMessage orderMessage)
    {
        List<OrderMessage> list = orderMessageService.selectOrderMessageList(orderMessage);
        ExcelUtil<OrderMessage> util = new ExcelUtil<OrderMessage>(OrderMessage.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(orderMessageService.selectOrderMessageByOrderId(orderId));
    }

    /**
     * 查询某人的下单情况
     * @param wxId
     * @return
     */
    @GetMapping("selectOrderByWxId/{wxId}")
    public AjaxResult selectOrderByWxId(@PathVariable("wxId") String wxId) {
        return AjaxResult.success(orderMessageService.selectOrderByWxId(wxId));
    }

    /**
     * 新增订单信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:order:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderMessage orderMessage)
    {
        return toAjax(orderMessageService.insertOrderMessage(orderMessage));
    }

    /**
     * 新增订单信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:order:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping("/addByWxId")
    public AjaxResult addByWxId(@RequestBody OrderMessage orderMessage)
    {
        return orderMessageService.insertOrderMessageByWxId(orderMessage) > 0 ? AjaxResult.success("添加成功", orderMessage) : AjaxResult.error("添加失败");
    }

    /**
     * 修改订单信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:order:edit')")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderMessage orderMessage)
    {
        return toAjax(orderMessageService.updateOrderMessage(orderMessage));
    }

    /**
     * 删除订单信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:order:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(orderMessageService.deleteOrderMessageByOrderIds(orderIds));
    }

    /**
     * 结账
     * @param orderId
     * @param score
     * @return
     */
    @PutMapping("/checkOut/{orderId}/{score}")
    public AjaxResult checkOut(@PathVariable("orderId") Long orderId, @PathVariable("score") Integer score) {
        return toAjax(orderMessageService.checkOut(orderId, score));
    }
}
